class Solution {
    public int deleteAndEarn(int[] nums) {
        int n= nums.length;
        int[] arr=new int[10001];
        for (int i = 0; i < n; i++) {
            arr[nums[i]]+=nums[i];
        }
        int[] f=new int[10001];
        int[] g=new int[10001];

        f[0]=arr[0];
        for (int i = 1; i < 10001; i++) {
            f[i]=g[i-1]+arr[i];
            g[i]=Math.max(f[i-1],g[i-1]);
        }
        return Math.max(f[10000],g[10000]);
    }
}